//
//  HTWaybillHomeViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/28.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTWaybillHomeViewController.h"
#import "HTWaybillCell.h"
#import "HTWaybillDetialViewController.h"
#import "HTWaybillModel.h"
@interface HTWaybillHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign) NSInteger selectedIndex;
@property (weak, nonatomic) IBOutlet UIView *tapAllView;
@property (weak, nonatomic) IBOutlet UIView *lineAll;
@property (weak, nonatomic) IBOutlet UILabel *allLabel;

@property (weak, nonatomic) IBOutlet UIView *tapContinueView;
@property (weak, nonatomic) IBOutlet UIView *lineContinue;
@property (weak, nonatomic) IBOutlet UILabel *labelContinue;

@property (weak, nonatomic) IBOutlet UIView *tapOverView;
@property (weak, nonatomic) IBOutlet UIView *lineOver;
@property (weak, nonatomic) IBOutlet UILabel *overLabel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@end

@implementation HTWaybillHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTapGesture];
    [self configTable];
   
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)configTable{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.title = @"我的运单";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTWaybillCell" bundle:nil] forCellReuseIdentifier:waybillCell];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kBackgroundColor;
    [self configTableHeader];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无运单";
   
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)didTapItem:(UITapGestureRecognizer *)sender{
    switch ([sender view].tag) {
        case 83://全部
        {
            if (self.selectedIndex == 0) {
                return;
            }
            self.selectedIndex = 0;
            [self setTapOneSelected];
            [self.tableView.mj_header beginRefreshing];
        }
            break;
        case 84://进行中
        {
            if (self.selectedIndex == 1) {
                return;
            }
            self.selectedIndex = 1;
            [self setTapTwoSelected];
            [self.tableView.mj_header beginRefreshing];
        }
            break;
        case 85://已完成
        {
            if (self.selectedIndex == 3) {
                return;
            }
            self.selectedIndex = 3;
            [self setTapThreeSelected];
            [self.tableView.mj_header beginRefreshing];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark --- tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   HTWaybillCell *cell = (HTWaybillCell *)[tableView dequeueReusableCellWithIdentifier:waybillCell forIndexPath:indexPath withController:self];
    cell.waybillModel = self.dataSource[indexPath.row];
    __weak typeof(self) WeakSelf = self;
    cell.detialBlock = ^(HTWaybillModel *wayBillModel) {
        HTWaybillDetialViewController *detial = [[HTWaybillDetialViewController alloc]init];
        detial.waybillModel = wayBillModel;
        [WeakSelf.navigationController pushViewController:detial animated:YES];
    };
    __weak typeof(UITableView) *WeakTableView = self.tableView;
    cell.refreshBlock = ^{
        [WeakTableView.mj_header beginRefreshing];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 136;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTWaybillDetialViewController *detial = [[HTWaybillDetialViewController alloc]init];
    detial.waybillModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detial animated:YES];
}
#pragma mark --- 绘制上面的Bar条
- (void)addTapGesture{
     self.selectedIndex = 0;
    [self setTapOneSelected];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapItem:)];
    self.tapAllView.tag = 83;
    [self.tapAllView addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapItem:)];
    self.tapContinueView.tag = 84;
    [self.tapContinueView addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapItem:)];
    self.tapOverView.tag = 85;
    [self.tapOverView addGestureRecognizer:tap3];
}
- (void)setTapOneSelected{
    [self tapAllColor:kBlueColor ];
    [self tapContinueColor:kMainGrayColor];
    [self tapOverColor:kMainGrayColor];
    self.lineAll.backgroundColor = kBlueColor;
     self.lineContinue.backgroundColor = [UIColor whiteColor];
    self.lineOver.backgroundColor = [UIColor whiteColor];
}
- (void)setTapTwoSelected{
    [self tapAllColor: kMainGrayColor];
    [self tapContinueColor:kBlueColor];
    [self tapOverColor:kMainGrayColor];
    self.lineContinue.backgroundColor = kBlueColor;
    self.lineAll.backgroundColor = [UIColor whiteColor];
    self.lineOver.backgroundColor = [UIColor whiteColor];
}
- (void)setTapThreeSelected{
    [self tapAllColor: kMainGrayColor ];
    [self tapContinueColor:kMainGrayColor];
    [self tapOverColor:kBlueColor];
    self.lineOver.backgroundColor = kBlueColor;
    self.lineAll.backgroundColor = [UIColor whiteColor];
    self.lineContinue.backgroundColor = [UIColor whiteColor];
}
- (void)tapAllColor:(UIColor *)color{
    self.allLabel.textColor = color;
}
- (void)tapContinueColor:(UIColor *)color{
    self.labelContinue.textColor = color;
}
- (void)tapOverColor:(UIColor *)color{
    self.overLabel.textColor = color;
}
#pragma mark --- 网络请求
- (void)loadNewData{
    [self netLoadWaybill];
}
- (void)netLoadWaybill{
    
    [HTHttpRequestManager requestWayBillsWithState:[NSString stringWithFormat:@"%ld",self.selectedIndex] success:^(id  _Nullable responseObject) {
        DBLog(@"运单列表：%@",responseObject);
        [self.tableView.mj_header endRefreshing];
        if (!responseObject) {
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            return;
        }
        [self.dataSource removeAllObjects];
        self.dataSource = [HTWaybillModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}




@end
